package com.smartwebee.android.blespp.blockly.js;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.smartwebee.android.blespp.blockly.BlockScanActivity;
import com.smartwebee.android.blespp.blockly.base.BlockBaseActivity;
import com.smartwebee.android.blespp.blockly.dialog.TipsDialog;
import com.tencent.smtt.sdk.WebView;

public class JsInterface {
    public static String TAG = "JsInterface";
    private String PREFERENCES_NAME = "preferences_name";

    public BlockBaseActivity activity;
    public WebView webView;

    public JsInterface(BlockBaseActivity activity, WebView webView) {
        this.activity = activity;
        this.webView = webView;
    }


    /**
     * 发送命令
     * @param code
     */
    @JavascriptInterface
    public void writeToDevice(String code) {
        if (TextUtils.isEmpty(activity.mDeviceAddress)) {
            showDialog();
        } else {
            activity.sendDataToDevice(code);
        }
    }

    /**
     * 发送调试命令
     * @param code
     */
    @JavascriptInterface
    public void writeDebugStrToDevice(String code) {
        if (TextUtils.isEmpty(activity.mDeviceAddress)) {
            showDialog();
        } else {
            activity.sendDebugDataToDevice(code);
        }
    }

    @JavascriptInterface
    public void saveStr(String key, String value) {
        SharedPreferences.Editor editor = getSPEditor();
        editor.putString(key, value);
        editor.commit();
    }

    @JavascriptInterface
    public void deleteStr(String key) {
        saveStr(key, "");
    }

    @JavascriptInterface
    public void getStr(final String key) {
        final SharedPreferences sp = activity.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    String jsStr = "javascript:DemoApp.programList.onGetKey('" + key + "', '" + sp.getString(key, "") + "')";
                    webView.evaluateJavascript(jsStr, null);
                }
            }
        });

    }

    private SharedPreferences.Editor getSPEditor() {
        SharedPreferences sp = activity.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        return editor;
    }

    public void showDialog() {
        TipsDialog tipsDialog = new TipsDialog();
        tipsDialog.setTitle("蓝牙未连接");
        tipsDialog.setContent("请连接您的蓝牙设备");
        tipsDialog.setLeftStr("稍后");
        tipsDialog.setRightStr("马上连接");
        tipsDialog.setClickEvent(new TipsDialog.ClickEvent() {
            @Override
            public void clickLeft() {

            }

            @Override
            public void clickRight() {
                activity.startActivity(new Intent(activity, BlockScanActivity.class));
            }
        });
        tipsDialog.show(activity.getFragmentManager(), TipsDialog.TAG);
    }

}
